<template>
  <div class="shopcar-container">
    <div class="goods-list">
      <div class="mui-card">
        <div class="mui-card-content">
          <div class="mui-card-content-inner" v-for="(item,index) in goodslist" :key="item.id">
            <mt-switch v-model="$store.getters.getGoodsSelected[item.id] " @change="selectedChanged(item.id,$store.getters.getGoodsSelected[item.id])"></mt-switch>
            <img :src="item.src" alt />
            <div class="info">
              <h1>{{item.title}}</h1>
              <p>
                <span class="price">￥{{item.sellPrice}}</span>
                <!-- <numbox :initcount="$store.getters.getGoodsCount[item.id]" :goodsid="item.id"></numbox> -->
                <numbox :initcount="$store.getters.getGoodsCount[item.id]" :goodsid="item.id"></numbox>
                <a href="#" @click.prevent="remove(item.id,index)">删除</a>
              </p>
            </div>
          </div>
        </div>
      </div>
      <div class="mui-card">
        <div class="mui-card-content">
          <div class="mui-card-content-inner jiesuan">
            <div class="left">
              <p>总计（不含运费）</p>
              <p>
                已勾选商品
                <span class="red">{{$store.getters.getGoodsCountAndAmount.count}}</span>&nbsp;件，总价
                <span class="red">￥{{$store.getters.getGoodsCountAndAmount.amount}}</span>
              </p>
            </div>
            <mt-button type="danger">去结算</mt-button>
          </div>
        </div>
      </div>
    </div>
    <p>{{$store.getters.getGoodsSelected}}</p>
  </div>
</template>

<script>
import numbox from "../components/Shopcar_numbox.vue";

export default {
  data() {
    return {
      goodslist: [] //购物车所有商品的数据
    };
  },
  created() {
    this.getGoodsList();
  },
  methods: {
    //获取store中所有商品的id，找数据
    getGoodsList() {
      var idArr = [];
      this.$store.state.car.forEach(item => {
        idArr.push(item.id);
      });
      console.log(idArr.join(","));
      if (idArr.length <= 0) {
        return;
      }
      //找数据
      this.$axios.get("../json/goodslist.json").then(res => {
        this.goodslist = res.data;
        console.log(this.goodslist);
        //console.log(this.$store.getters.getGoodsCount);
      });
    },
    remove(id, index) {
      this.goodslist.splice(index, 1);
      this.$store.commit("remove", id);
    },
    selectedChanged(id,val){
      //每当点击开关，把最新状态同步到store中
      this.$store.commit("updateGoodsSelected",{id:id,selected:val});
    }
  },
  components: {
    numbox
  }
};
</script>
<style lang="scss" scoped>
.shopcar-container {
  background-color: #eee;
  overflow: hidden;
  .goods-list {
    .mui-card-content-inner {
      display: flex;
      align-items: center;
    }
    img {
      width: 60px;
      height: 60px;
    }
    h1 {
      font-size: 13px;
      text-align: center;
    }
    .info {
      a {
        margin: 5px;
      }
      .price {
        color: red;
        font-weight: bold;
        margin: 5px;
      }
    }
  }
  .jiesuan {
    display: flex;
    justify-content: space-between;
    align-items: center;
    .red {
      color: red;
      font-weight: bold;
    }
  }
}
</style>
